package com.easycms.framework.web;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.easycms.constant.JacksonConstants;
import com.easycms.framework.domain.DataTableDto;
import com.easycms.framework.domain.ResultAdaptDto;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import java.beans.PropertyEditorSupport;
import java.util.Date;
import java.util.Hashtable;

public class BaseController {

    @InitBinder
    protected void initBinder(WebDataBinder binder) {
        binder.registerCustomEditor(Date.class, new PropertyEditorSupport() {
            @Override
            public void setAsText(String text) {
                setValue(DateUtil.parse(text));
            }
        });
        binder.registerCustomEditor(Integer.class, new PropertyEditorSupport() {
            @Override
            public void setAsText(String text) {
                if(StrUtil.isBlank(text)){
                    text="0";
                }
                setValue(Convert.toInt(text,0));
            }
        });
        binder.registerCustomEditor(int.class, new PropertyEditorSupport() {
            @Override
            public void setAsText(String text) {
                if(StrUtil.isBlank(text)){
                    text="0";
                }
                setValue(Convert.toInt(text,0));
            }
        });
        binder.registerCustomEditor(long.class, new PropertyEditorSupport() {
            @Override
            public void setAsText(String text) {
                if(StrUtil.isBlank(text)){
                    text="0";
                }
                setValue(Convert.toLong(text,0L));
            }
        });
    }


    /**
     * 返回页面
     */
    //public String view(String url){
     //   return url;
    //}
    /**
     * 页面跳转
     */
    public String redirect(String url)
    {

        return StrUtil.format("redirect:{}", url);
    }

    public ActionResult redirectTo(String url)
    {
        return view(StrUtil.format("redirect:{}", url));
    }
    /**
     *
     * @author yushuo
     * @description //返回视图view
     * @date 14:15 2021/2/4
     * @param
     * @return com.easycms.framework.web.ActionResult
     **/
    protected ActionResult view() {

        return view(null);
    }
    /**
     *
     * @author yushuo
     * @description //返回视图view
     * @date 14:15 2021/2/4
     * @param [viewpath]
     * @return com.easycms.framework.web.ActionResult
     **/
    protected ActionResult view(String viewPath) {
        return view(viewPath,null);
    }
    /**
     *
     * @author yushuo
     * @description //返回视图view
     * @date 14:15 2021/2/4
     * @param [model]
     * @return com.easycms.framework.web.ActionResult
     **/
    protected ActionResult view(Object model) {
        return view(null, model);
    }

    /**
    *
     * @author yushuo
     * @description //返回视图view
     * @date 14:15 2021/2/4
     * @param [viewpath, model]
     * @return com.easycms.framework.web.ActionResult
    **/
    protected ActionResult view(String viewpath, Object model) {
        ActionResult r = new ActionResult();
        r.setViewName(viewpath);
        r.addObject("model", model);
        return r;
    }


    /**
    *
     * @author yushuo
     * @description //返回json
     * @date 14:15 2021/2/4
     * @param  [Hashtable<String,Object> data]
     * @return org.springframework.web.servlet.ModelAndView
    **/
    protected ActionResult json(Hashtable<String,Object> data) {
        ActionResult modelAndView = new ActionResult(JacksonConstants.mappingJackson2JsonView);
        modelAndView.addObject("message", null);
        modelAndView.addObject("code",0);
        modelAndView.addObject("status",true);
        modelAndView.addObject("data", data);
        return modelAndView;
    }

    /**
     *
     * @author yushuo
     * @description //返回json
     * @date 14:15 2021/2/4
     * @param  [ResultAdaptDto data]
     * @return org.springframework.web.servlet.ModelAndView
     **/
    protected ActionResult json(ResultAdaptDto data) {
        ActionResult modelAndView = new ActionResult(JacksonConstants.mappingJackson2JsonView);
        modelAndView.addObject("message", data.getMessage());
        modelAndView.addObject("code",data.getCode());
        modelAndView.addObject("status",data.isStatus());
        modelAndView.addObject("data", data.getData());
        return modelAndView;
    }

    /**
     *
     * @author yushuo
     * @description //返回json
     * @date 14:15 2021/2/4
     * @param  [DataTableDto data]
     * @return org.springframework.web.servlet.ModelAndView
     **/
    protected ActionResult json(DataTableDto data) {
        ActionResult modelAndView = new ActionResult(JacksonConstants.mappingJackson2JsonView);
        modelAndView.addObject(data);
        modelAndView.addObject("pageNumber", data.getPageNumber());
        modelAndView.addObject("pageSize",data.getPageSize());
        modelAndView.addObject("total",data.getTotal());
        modelAndView.addObject("rows", data.getRows());
        return modelAndView;
    }

    /**
    *
     * @author yushuo
     * @description //请求失败
     * @date 14:20 2021/2/4
     * @param [message]
     * @return com.easycms.framework.web.ActionResult
    **/
    protected ActionResult error(String message){
        ActionResult modelAndView = new ActionResult(JacksonConstants.mappingJackson2JsonView);
        modelAndView.addObject("message", message);
        modelAndView.addObject("code",0);
        modelAndView.addObject("status",false);
        return modelAndView;
    }

    /**
     *
     * @author yushuo
     * @description //请求失败
     * @date 14:20 2021/2/4
     * @param
     * @return com.easycms.framework.web.ActionResult
     **/
    protected ActionResult error(){
        ActionResult modelAndView = new ActionResult(JacksonConstants.mappingJackson2JsonView);
        modelAndView.addObject("message", "操作失败");
        modelAndView.addObject("code",0);
        modelAndView.addObject("status",false);
        return modelAndView;
    }

    /**
     *
     * @author yushuo
     * @description //请求失败
     * @date 14:20 2021/2/4
     * @param [message]
     * @return com.easycms.framework.web.ActionResult
     **/
    protected ActionResult ok(String message){
        ActionResult modelAndView = new ActionResult(JacksonConstants.mappingJackson2JsonView);
        modelAndView.addObject("message", message);
        modelAndView.addObject("code",0);
        modelAndView.addObject("status",true);
        return modelAndView;
    }

    /**
     *
     * @author yushuo
     * @description //请求成功
     * @date 14:20 2021/2/4
     * @param
     * @return com.easycms.framework.web.ActionResult
     **/
    protected ActionResult ok(){
        ActionResult modelAndView = new ActionResult(JacksonConstants.mappingJackson2JsonView);
        modelAndView.addObject("message", "操作成功");
        modelAndView.addObject("code",0);
        modelAndView.addObject("status",true);
        return modelAndView;
    }



}
